﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Quartz.Api.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using static Quartz.Api.GlobalVar.CustomApiVersion;

namespace Quartz.Api.ServiceExtensions
{
    public static class SwaggerSetup
    {
        public static void AddSwaggerSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            var basePath = AppContext.BaseDirectory;
            var ApiName = Appsettings.app("ApiName");

            services.AddSwaggerGen(c =>
            {
                //遍历出全部的版本，做文档信息展示
                typeof(ApiVersions).GetEnumNames().ToList().ForEach(version =>
                {
                    c.SwaggerDoc(version, new OpenApiInfo
                    {
                        Version = version,
                        Title = $"{ApiName} 接口文档——{RuntimeInformation.FrameworkDescription}",
                        Description = $"{ApiName} HTTP API " + version,
                        License = new OpenApiLicense { Name = ApiName + "博客园", Url = new Uri("https://www.cnblogs.com/") }
                    });

                    c.OrderActionsBy(o => o.RelativePath);
                    //按代码编写顺序排序
                    c.OrderActionsBy(p => p.ToString());
                });

                try
                {
                    //这个就是刚刚配置的xml文件名
                    var xmlPath = Path.Combine(basePath, "Quartz.Api.xml");
                    //默认的第二个参数是false，这个是controller的注释，记得修改
                    c.IncludeXmlComments(xmlPath, true);
                }
                catch (Exception ex)
                {
                    //NLogHelper.Error("Voc.Eval.xml和Voc.Eval.Model.xml 丢失，请检查并拷贝。\n" + ex.Message);
                }
            });
        }
    }
}
